precision mediump float;

uniform vec2 u_resolution;
// 定义SDF函数，计算点(x, y)到直线段的有符号距离
float lineSegmentSDF(vec2 p, vec2 a, vec2 b) {
    vec2 pa = p - a;
    vec2 ba = b - a;
    float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
    return length(pa - ba * h);
}

void main() {
    vec2 res = vec2(800.0, 600.0); // 窗口大小
    vec2 uv = (gl_FragCoord.xy * 2. - u_resolution) / u_resolution;
    
    // 线段端点坐标
    vec2 start = vec2(-0.5, -0.5);
    vec2 end = vec2(0.5, 0.5);
    
    // 计算当前像素点到线段的有符号距离
    float d = lineSegmentSDF(uv, start, end);
    
    // 设置线段宽度
    float lineWidth = 0.02;
    
    // 根据距离和线段宽度进行着色
    float alpha = smoothstep(0.0, lineWidth, d);
    vec3 color = vec3(1.0);
    
    // 输出颜色值
    gl_FragColor = vec4(color, alpha);
}
